package model;

import utils.Formatter;
import utils.EnumType.UnitInfo;
import model.exception.MacAddressException;

/**
 * @author Dumella De Rosa Gennaro
 *
 */
public class XenNetworkInterface extends NetworkInterface{
	
	
	/**
	 * Nome dell'interfaccia virtuale 
	 */
	private String targetDev;
	
	/**
	 * Tipo di connessione delle interfacce virtuali. Al momento solo la connesione tramite 
	 * bridge è supportata.
	 */
	public enum Type {bridge};
	private Type interfaceType;
	
	/**
	 * Nome del soft-bridge (ex. eth0,eth1,..) a cui dovrà essere collegata l'interfaccia
	 */
	private String sourceBridge;
	
	/**
	 * Percorso dello script per controllare le interfacce virtuali. Se viene specificato sovrascrive
	 * il valore di default presente nel file xend-config.sxp. Lo script vif-bridge è associato con lo
	 * script network-bridge, lo script vif-route è associato con lo script network-route, e lo script 
	 * vif-nat è associato con lo script vif-nat. 
	 */
	private String scriptPath;
	
	/**
	 * Rate trasmissivo massimo
	 */
	private Bandwidth bandwidth;

	
	public XenNetworkInterface(String targetDevice,Type interfaceType,MacAddress mac,
			String sourceBridge,String scriptPath,Bandwidth bandwidth ) {
		super(targetDevice,mac);
		this.interfaceType = interfaceType;
		this.sourceBridge = sourceBridge;
		this.scriptPath = scriptPath;
		this.bandwidth = bandwidth;
	}
	

	
	public String getXMLDescription(){
		return getXMLLibvirtDescription();
	}
	
	private String getXMLLibvirtDescription()
	{
		String xmlDesc ="<interface type='"  +this.interfaceType+			"'>\n" +
						"	<mac address='"  +super.macAddress+				"'/>\n" +
						"	<source bridge='"+this.sourceBridge+			"'/>\n" +
						"	<script path='"  +this.scriptPath+				"'/>\n" +
						"	<target dev='"   +this.getName()+				"'/>\n";

		if ( this.bandwidth.getUnit() != null && this.bandwidth.getPeriod() != null && 
				rateIsValid(this.bandwidth) )
			{
				xmlDesc += 	"	<networkresource> \n" +
							"		<capped-bandwidth unit='"+this.bandwidth.getUnit()+"' period='"+this.bandwidth.getPeriod()+"' value='"+Formatter.DecimalFormatter(this.bandwidth.getValue())+"'/>\n" +
							"	</networkresource> \n";
			}
		xmlDesc += "</interface> \n";
		return xmlDesc;
	}
	
	private boolean rateIsValid(Bandwidth bandwidth){
		if (bandwidth != null)
			return true;
		return false;
	}
	
	public String getTargetDevice() {
		return targetDev;
	}
	
	public Type getInterfaceType() {
		return interfaceType;
	}

	public String getSourceBridge() {
		return sourceBridge;
	}

	public String getScriptPath() {
		return scriptPath;
	}

	public Bandwidth getRate() {
		return bandwidth;
	}

	public void setInterfaceType(Type interfaceType) {
		this.interfaceType = interfaceType;
	}

	public void setSourceBridge(String sourceBridge) {
		this.sourceBridge = sourceBridge;
	}

	public void setScriptPath(String scriptPath) {
		this.scriptPath = scriptPath;
	}

	public void setRate(Bandwidth bandwidth) {
		this.bandwidth = bandwidth;
	}
	
	
	public static void main(String[] args) throws MacAddressException {
		
		XenNetworkInterface xenNetInterface = new XenNetworkInterface("id01eth1",Type.bridge,
				new MacAddress("00:16:3E:01:02:01"),"eth1","vif-bridge",
				new Bandwidth(UnitInfo.kilobit,256));
		
		System.out.println(xenNetInterface.getXMLDescription());
		
	}
}
